const Home = require('../db/models/homeSchema')
const { successModel, errorModel, code } = require("../utils/stateModel")

// 首页面 banner 图数据请求接口
const queryInfo = async (ctx, params) => {
  try {
    const list = await Home.find({}) || []
    ctx.body = new successModel(list, "查询成功")
  } catch (err) {
    ctx.body = new errorModel("查询失败", code.DB_ERROR)
  }
}

// 首页面中 banner 图编辑请求接口
const updateHomeList = async (ctx, { _id, ...params }) => {
  try {
    const homeInfo = await Home.findOneAndUpdate({ _id }, { ...params })
    if (homeInfo) {
      ctx.body = new successModel("修改成功")
      return
    }
    ctx.body = new errorModel("修改失败")
  } catch (error) {
    ctx.body = new errorModel(error.message)
  }
}

module.exports = { queryInfo, updateHomeList }